package com.enba.boot.mq;

import com.enba.boot.mq.factory.MqStrategyFactory;
import com.enba.boot.mq.properties.MqProperties;
import java.util.function.Supplier;

public class MqHelper {

  private final MqStrategyFactory mqStrategyFactory;
  private final MqProperties mqProperties;

  public MqHelper(MqStrategyFactory mqStrategyFactory, MqProperties mqProperties) {
    this.mqStrategyFactory = mqStrategyFactory;
    this.mqProperties = mqProperties;
  }

  /**
   * 发送队列消息
   *
   * @param destinationName 队列名称
   * @param message 消息
   * @param supplier 回调函数
   * @param <T> 回调函数返回类型
   * @return 回调函数返回值
   */
  public <T> T sendMessageByQueue(String destinationName, String message, Supplier<T> supplier) {
    if (mqProperties.getActivemq().isEnable()) {
      // 获取发送mq策略实现
      MessageSenderStrategy<?> messageSenderStrategy =
          mqStrategyFactory.getMessageSenderStrategy(mqProperties.getActivemq().getStrategyKey());

      // 发送消息
      messageSenderStrategy.sendMessageByQueue(destinationName, message);
    }

    if (mqProperties.getKafka().isEnable()) {
      // 获取发送mq策略实现
      MessageSenderStrategy<?> messageSenderStrategy =
          mqStrategyFactory.getMessageSenderStrategy(mqProperties.getKafka().getStrategyKey());

      // 发送消息
      messageSenderStrategy.sendMessageByQueue(destinationName, message);
    }

    if (mqProperties.getRabbitmq().isEnable()) {
      // 获取发送mq策略实现
      MessageSenderStrategy<?> messageSenderStrategy =
          mqStrategyFactory.getMessageSenderStrategy(mqProperties.getRabbitmq().getStrategyKey());

      // 发送消息
      messageSenderStrategy.sendMessageByQueue(destinationName, message);
    }

    if (mqProperties.getRocketmq().isEnable()) {
      // 获取发送mq策略实现
      MessageSenderStrategy<?> messageSenderStrategy =
          mqStrategyFactory.getMessageSenderStrategy(mqProperties.getRocketmq().getStrategyKey());

      // 发送消息
      messageSenderStrategy.sendMessageByQueue(destinationName, message);
    }

    return supplier.get();
  }

  /**
   * 发送主题消息
   *
   * @param topicName 主题名称
   * @param message 消息
   * @param supplier 回调函数
   * @param <T> 回调函数返回类型
   * @return 回调函数返回值
   */
  public <T> T sendMessageByTopic(String topicName, String message, Supplier<T> supplier) {
    if (mqProperties.getActivemq().isEnable()) {
      // 获取发送mq策略实现
      MessageSenderStrategy<?> messageSenderStrategy =
          mqStrategyFactory.getMessageSenderStrategy(mqProperties.getActivemq().getStrategyKey());

      // 发送消息
      messageSenderStrategy.sendMessageByTopic(topicName, message);
    }

    if (mqProperties.getKafka().isEnable()) {
      // 获取发送mq策略实现
      MessageSenderStrategy<?> messageSenderStrategy =
          mqStrategyFactory.getMessageSenderStrategy(mqProperties.getKafka().getStrategyKey());

      // 发送消息
      messageSenderStrategy.sendMessageByTopic(topicName, message);
    }

    if (mqProperties.getRabbitmq().isEnable()) {
      // 获取发送mq策略实现
      MessageSenderStrategy<?> messageSenderStrategy =
          mqStrategyFactory.getMessageSenderStrategy(mqProperties.getRabbitmq().getStrategyKey());

      // 发送消息
      messageSenderStrategy.sendMessageByTopic(topicName, message);
    }

    if (mqProperties.getRocketmq().isEnable()) {
      // 获取发送mq策略实现
      MessageSenderStrategy<?> messageSenderStrategy =
          mqStrategyFactory.getMessageSenderStrategy(mqProperties.getRocketmq().getStrategyKey());

      // 发送消息
      messageSenderStrategy.sendMessageByTopic(topicName, message);
    }

    return supplier.get();
  }

  public <T> T sendMessageByTopic(
      String topicName, String message, String key, Supplier<T> supplier) {
    if (mqProperties.getActivemq().isEnable()) {
      // 获取发送mq策略实现
      MessageSenderStrategy<?> messageSenderStrategy =
          mqStrategyFactory.getMessageSenderStrategy(mqProperties.getActivemq().getStrategyKey());

      // 发送消息
      //      messageSenderStrategy.sendMessageByTopic(topicName, message);
    }

    if (mqProperties.getKafka().isEnable()) {
      // 获取发送mq策略实现
      MessageSenderStrategy<?> messageSenderStrategy =
          mqStrategyFactory.getMessageSenderStrategy(mqProperties.getKafka().getStrategyKey());

      // 发送消息
      //      messageSenderStrategy.sendMessageByTopic(topicName, message);
    }

    if (mqProperties.getRabbitmq().isEnable()) {
      // 获取发送mq策略实现
      MessageSenderStrategy<?> messageSenderStrategy =
          mqStrategyFactory.getMessageSenderStrategy(mqProperties.getRabbitmq().getStrategyKey());

      // 发送消息
      //      messageSenderStrategy.sendMessageByTopic(topicName, message);
    }

    if (mqProperties.getRocketmq().isEnable()) {
      // 获取发送mq策略实现
      MessageSenderStrategy<?> messageSenderStrategy =
          mqStrategyFactory.getMessageSenderStrategy(mqProperties.getRocketmq().getStrategyKey());

      // 发送消息
      messageSenderStrategy.sendMessageByTopic(topicName, message, key);
    }

    return supplier.get();
  }
}
